package com.kou.backend.config;

import com.kou.backend.enums.StorageTypeEnum;
import com.kou.backend.property.MinioProperties;
import com.kou.backend.service.FileStorageService;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: kou
 * @since: 2025/6/26 17:40
 */
@Configuration
public class FileStorageConfig {
    @Value("${file.storage.type}")
    private String storageType;

    @Autowired
    private ApplicationContext context;

    @Bean
    public FileStorageService fileStorageService() {
        if (StorageTypeEnum.MINIO.getStorageType().equalsIgnoreCase(storageType)) {
            return context.getBean("minioFileStorageService", FileStorageService.class);
        } else {
            return context.getBean("localFileStorageService", FileStorageService.class);
        }
    }

    @Bean
    public MinioClient minioClient(MinioProperties minioProperties) {
        return MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
    }
}
